<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################

mysql_connect($sqlserver,$sqluser,$sqlpass) or alert("AdtrackzGold couldn't connect to the mysql server.");
mysql_select_db($sqldb) or alert("AdtrackzGold couldn't select the mysql database.");

function populate_tables()
{
	create_campaigns();
	create_subcampaigns();
	create_groups();
	create_clicks();
	create_actions();
	create_sales();
	create_alerts();
	create_redirects();
	create_splittest();
	create_preferences();
	create_login();
}
	


/* CAMPAIGNS */
//to hold campaign info
//type = clicks, actions, sales, all
/*
Table Changes:
-adcode from size 30 to 100
-url from 100 to 255
-note: ppc is used to check if ppc is on or off, you can't switch from a 
ppc campaign cost type to non-ppc type, it would mess up the stats
*/
function create_campaigns()
{
	$query = "CREATE TABLE adtrackz_campaigns ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	groupID int(11),
	adcode varchar(100),
	url varchar(255),
	cost double default 0,
	startdate datetime,
	enddate datetime,
	description varchar(255),
	purpose varchar(10),
	recurdays int(5),
	repeat int(5),
	ppc int(1),
	INDEX campaigns (ID)
	)";

	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_campaigns: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}

	$query = "CREATE TABLE adtrackz_campaigns_st ( 
	campaignID int(11),
	st varchar(10)
	)";

	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_campaigns_st: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}


/* SUB CAMPAIGNS */
//to hold sub campaign info

/*
-I decided to have purpose, group, status, time period same as parent campaign
to simplify the stats.php code
-i'm not sure about the cost variables though
*/
function create_subcampaigns()
{
	$query = "CREATE TABLE adtrackz_subcampaigns ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	campaignID int(11),
	adcode varchar(100),
	url varchar(255),
	cost double default 0,
	description varchar(255),
	ppc int(1),
	INDEX cID (campaignID)
	)";

	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_subcampaigns: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}


/* GROUPS */
// campaign groups/types
function create_groups()
{
	$query = "CREATE TABLE adtrackz_groups ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	groupname varchar(50)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_groups: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}

/*CLICKS */
// logs all clicks
/*
Table Changes:
-subcampaign is now subID and i moved places with it, this may impact a lot of the code
-
-
*/
function create_clicks()
{
	$query = "CREATE TABLE adtrackz_clicks ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	campaignID int(11),
	subID int(11),
	timest int(11),
	referringurl varchar(255),
	uniqueflag int(1),
	cpc double default 0,
	url varchar(255),
	INDEX cID (campaignID),
	INDEX sID (subID),
	INDEX timestindex (timest)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_clicks: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}


/* ACTIONS */
// track actions
function create_actions()
{
	$query = "CREATE TABLE adtrackz_actions ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	campaignID int(11),
	subID int(11),
	timest int(11),
	inputID varchar(20),
	name varchar(50),
	description varchar(100),
	INDEX cID (campaignID),
	INDEX sID (subID),
	INDEX timestindex (timest)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_actions: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}

/* SALES */
// track sales
function create_sales()
{
	$query = "CREATE TABLE adtrackz_sales ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	campaignID int(11),
	subID int(11),
	timest int(11),
	revenue double,
	inputID varchar(20),
	name varchar(50),
	description varchar(100),
	INDEX cID (campaignID),
	INDEX sID (subID),
	INDEX timestindex (timest)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_sales: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}


/* ALERTS */
// keep track of lost hits from user errors or
// other reasons
function create_alerts()
{
	$query = "CREATE TABLE adtrackz_alerts ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	invadcode varchar(50),
	invsub varchar(50),
	timest DATETIME,
	referringurl varchar(255),
	reason varchar(255),
	INDEX times (timest)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_alerts: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}

/* REDIRECT */
// to keep track of which redirects were created?
function create_redirects()
{
	$query = "CREATE TABLE adtrackz_redirects ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	campaignID int(11),
	subcampaign varchar(15),	
	filename varchar(250),
	encrypt int(1),
	sticky int(1),	
	INDEX cID (campaignID)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_redirects: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}

/* SPLIT TESTS */
// to keep track of which redirects were created?
function create_splittest()
{
	$query = "CREATE TABLE adtrackz_split_test_groups ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	name varchar(20),
	tagvisitor varchar(8),
	startdate datetime
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_split_test_groups: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}

	$query = "CREATE TABLE adtrackz_split_test_campaigns ( 
	ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	campaignID int(11),
	split_test_ID int(11)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_split_test_campaigns: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}
}


/* PREFERENCES */
// to keep track of stats display preferences
function create_preferences()
{
	$query = "CREATE TABLE adtrackz_preferences ( 
	clicks varchar(7),
	rawclicks varchar(7),
	actions varchar(7),	
	sales varchar(7),	
	cpc varchar(7),	
	cpa varchar(7),	
	cps varchar(7),	
	cta varchar(7),	
	cts varchar(7),	
	cost varchar(7),	
	revenue varchar(7),	
	profit varchar(7),	
	roi varchar(7),
	purpose varchar(20),
	group1 varchar(20),
	status varchar(20),
	timeperiod varchar(20),
	column_purpose varchar(7),
	column_group varchar(7),	
	description varchar(7),
	campaign_display varchar(7),
	subcampaign_display varchar(7)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_preferences: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}

	//initiate them
	mysql_query("insert into adtrackz_preferences values ('checked', 'checked', 'checked', 'checked', 'checked', 'checked', 'checked', '', '',  'checked', 'checked', 'checked','checked','all purposes','all','all','today','','','','checked','checked')");
}

/* PREFERENCES */
// to keep track of stats display preferences
function create_login()
{
	global $username, $password, $email, $ftp_user, $ftp_pass, $default_url;

	$query = "CREATE TABLE adtrackz_login ( 
	username varchar(20),
	password varchar(20),
	email varchar(250),
	ftp_user varchar(100),
	ftp_pass varchar(50),
	default_url varchar(250)
	)";


	if (!mysql_query($query))
	{
		$error = mysql_error();
		alert("Couldn't create table adtrackz_login: $error. Please <a href=http://adtrackzgold.com>contact support</a>.");
		require "footer.php";
		exit;
	}

	//initiate them
	mysql_query("insert into adtrackz_login values ('$username', '$password', '$email', '$ftp_user', '$ftp_pass','$default_url')");
}

?>
